/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import Modelo.DAL.TablaNotificacionRiesgo_DAO;
import Modelo.TablaNotificacionRiesgo;
import java.io.PrintWriter;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.json.JSONObject;
import Acceso.Security;

/**
 *
 * @author Francis
 */
public class nuevoBloqNRAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador","Gestionar Notificaciones de Riesgo"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        response.setContentType("text/json");

        nuevoBloqNRForm formBean = (nuevoBloqNRForm) form;

        int idNR = formBean.getIdNR();
        String agentes = formBean.getAgentes();
        String efectos = formBean.getEfectos();
        String prevencion = formBean.getPrevencion();
        String medidasC = formBean.getMedidasC();
        String tipo = formBean.getTipo();

        ActionErrors errors = new ActionErrors();
        boolean error = false;

        if (idNR <= 0) {
            errors.add("idNR_nr_requerido", new ActionMessage("errors.idNR_nr_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (agentes == null || agentes.equals("")) {
            errors.add("agentes_rn_requerido", new ActionMessage("errors.agentes_rn_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (efectos == null || efectos.equals("")) {
            errors.add("efectos_rn_requerido", new ActionMessage("errors.efectos_rn_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (prevencion == null || prevencion.equals("")) {
            errors.add("prevencion_rn_requerido", new ActionMessage("errors.prevencion_rn_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (medidasC == null || medidasC.equals("")) {
            errors.add("medidasC_rn_requerido", new ActionMessage("errors.medidasC_rn_requerido"));
            saveErrors(request, errors);
            error = true;
        }
        if (tipo == null || tipo.equals("")) {
            errors.add("tipo_rn_requerido", new ActionMessage("errors.tipo_rn_requerido"));
            saveErrors(request, errors);
            error = true;
        }

        if (!error) {
            TablaNotificacionRiesgo tnr = new TablaNotificacionRiesgo(idNR, tipo, agentes, efectos, prevencion, medidasC);
            System.out.println("Entra: " + idNR + " - " + tipo);


            error = !TablaNotificacionRiesgo_DAO.insertarTNR(tnr);
            System.out.println("Sale: " + idNR + " - " + tipo + " - " + !error);

            formBean.setIdNR(-1);
            formBean.setAgentes(null);
            formBean.setEfectos(null);
            formBean.setPrevencion(null);
            formBean.setMedidasC(null);
            formBean.setTipo(null);
            System.out.print("Returning");
        }

        JSONObject nr = new JSONObject();
        nr.put("msg", !error);
        PrintWriter pw = response.getWriter();
        pw.write(nr.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
